package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootPersonSkill;
import org.footballmanager.pandafootball.system.form.impl.FootPersonSkillSearchForm;
import org.footballmanager.pandafootball.system.service.FootPersonSkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footPersonSkillSearchForm")
@RequestMapping("/foot/footPersonSkill")
public class FootPersonSkillController extends AddAndEditController<FootPersonSkill> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footPersonSkill";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.FOOT, ENTITYNAME);
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootPersonSkillService footPersonSkillService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                       参数
     * @param footPersonSkillSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootPersonSkillSearchForm footPersonSkillSearchForm) {
        FootPersonSkillSearchForm newFootPersonSkillSearchForm = new FootPersonSkillSearchForm();
        if (footPersonSkillSearchForm != null) {
            newFootPersonSkillSearchForm = footPersonSkillSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootPersonSkillSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootPersonSkillSearchForm footPersonSkillSearchForm, @RequestParam(required = false) Long id) {
        if (id != null) {
            footPersonSkillSearchForm.setQyId(id);
        }
        footPersonSkillService.search(ENTITYNAME, map, footPersonSkillSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footPersonSkillService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootPersonSkillSearchForm footPersonSkillSearchForm) {
        footPersonSkillService.deleteAll(footPersonSkillSearchForm, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map) {
        map.addAttribute(ENTITYNAME, new FootPersonSkill());
        return ADDPAGE;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footPersonSkillService.get(id));
        return EDITPAGE;
    }

    @RequestMapping(value = "/view")
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootPersonSkillSearchForm footPersonSkillSearchForm) {
        map.addAttribute(ENTITYNAME, footPersonSkillService.get(id));
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footPersonSkillSearchForm);
        return VIEWPAGE;
    }

}
